/*
 * 컴파일을 위해 올려 놓은 소스 입니다.
 * 서버에 반영하지 마시고 다른국가에 적용할 경우 서버에 있는 class 를 카피해서 사용하십시오.
 * 
 */
/*
 * Created on 2005. 1. 19.
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.pushmsg.OmaOtaConf.OmaOtaObject;

import java.util.Hashtable;

/**
 * @author dgchun
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DS  extends APPLICATION  
{
	final static private int NRESOURCE = 10;
	
	private RESOURCE[] resource = new RESOURCE[NRESOURCE];
	private String username = null;
	private String userpwd = null;
	private String name = null; //DS application name
	private int nResource = 0;
	private String address = null;
	
	String url = null;
	String country = null;
	String operator = null;
	String bearer = null;
	String nettype = null;
	
	private Hashtable profile = null;
	PXPHYSICAL physical = null;
	
	public DS(String country, String op, String bearer, String nettype)
	{ 
		super.appid = "w5";
		this.country = country;
		this.operator = op;
		this.bearer = bearer;
		this.nettype = nettype;		
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	public void setUserName(String name)
	{
		username = name;
	}
	
	public void setUserPwd(String pwd)
	{
		userpwd = pwd;
	}
	
	public void setResources(RESOURCE r)
	{
		resource[nResource++] = r;
	}
	
	public int getNresource()
	{
		return nResource;
	}	

	public void setProxyId(String id)
	{
		proxyid = id;
	}
	
	public void setNapId(String id)
	{
		napid = id;
	}
	
	public void setAddress(String address)
	{
		this.address = address;
	}
	
	private boolean getSetting()
	{
		String file = Constants.FILEPATH + country.toLowerCase() + "/" + operator + "_brow_" + bearer + "_" + nettype + ".xml";
		ProfileReader pr = new ProfileReader(file);
		profile = pr.getProfile();
		if(profile==null)
			return false;		
		url = (String)profile.get("URL");
		return true;		
	}

	public int getData(byte[] buffer, int index)
	{
		if(getSetting()==false)
			return -1;
		int startPos = index;

		//<characteristic type="APPLICATION">
		buffer[index++] = (byte)0xC6;
		buffer[index++] = (byte)0x00; //switch to 
		buffer[index++] = (byte)0x01; //code page 01
		buffer[index++] = (byte)0x55; //type="APPLICATION"
		buffer[index++] = (byte)0x01;
		//<parm name="APPID" value="w5"/>
		buffer[index++] = (byte)0x87;
		buffer[index++] = (byte)0x36; //name="APPID"
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(appid.getBytes(), 0, buffer, index, appid.length());
		index += appid.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of parm
		
		if(proxyid!=null)
		{
			//<parm name="TO-PROXY" value="xxx"/>
			buffer[index++] = (byte)0x87;
			buffer[index++] = (byte)0x39; //name="TO-PROXY"
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(proxyid.getBytes(), 0, buffer, index, proxyid.length());
			index += proxyid.length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of parm
			//<parm name="NAME" value="xxxx"/>
		}
		else
		{
			//<parm name="TO-NAPID" value="xxx"/>
			buffer[index++] = (byte)0x87;
			buffer[index++] = (byte)0x22; //name="TO-NAPID"
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(napid.getBytes(), 0, buffer, index, napid.length());
			index += napid.length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of parm
			//<parm name="NAME" value="xxxx"/>
		}
		//<parm name="NAME" value="xxx"/>
		buffer[index++] = (byte)0x87;
		buffer[index++] = (byte)0x07; //name="NAME"
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(name.getBytes(), 0, buffer, index, name.length());
		index += name.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of parm
		//<parm name="ADDR" value="xxx"/>
		buffer[index++] = (byte)0x87;
		buffer[index++] = (byte)0x34; //name="ADDR"
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(address.getBytes(), 0, buffer, index, address.length());
		index += address.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of parm
		
		for(int i=0; i<nResource; i++)
		{
			//<characteristic type="RESOURCE">
			buffer[index++] = (byte)0xC6;
			buffer[index++] = (byte)0x59; //type="RESOURCE"
			buffer[index++] = (byte)0x01;
			//<parm name="URI" value="xxxx"/>
			buffer[index++] = (byte)0x87;
			buffer[index++] = (byte)0x3A; //name="URI"
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(resource[i].getUri().getBytes(), 0, buffer, index, resource[i].getUri().length());
			index += resource[i].getUri().length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of parm		
			//<parm name="NAME" value="xxxx"/>
			buffer[index++] = (byte)0x87;
			buffer[index++] = (byte)0x07; //name="NAME"
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(resource[i].getName().getBytes(), 0, buffer, index, resource[i].getName().length());
			index += resource[i].getName().length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of parm
			//<parm name="AACCEPT" value="xxxx"/>
			buffer[index++] = (byte)0x87;
			buffer[index++] = (byte)0x2E; //name="AACCEPT"
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(resource[i].getAccept().getBytes(), 0, buffer, index, resource[i].getAccept().length());
			index += resource[i].getAccept().length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of parm			
			buffer[index++] = (byte)0x01; //end of characteristic with the attribute 'RESOURCE'
		}
		
		//<characteristic type="APPAUTH">
		buffer[index++] = (byte)0xC6;
		buffer[index++] = (byte)0x57; //type="APPAUTH"
		buffer[index++] = (byte)0x01;
		//<parm name="AAUTHNAME" value="xxxx"/>
		buffer[index++] = (byte)0x87;
		buffer[index++] = (byte)0x31; //name="AAUTHNAME"
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(username.getBytes(), 0, buffer, index, username.length());
		index += username.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of parm	
		//<parm name="AAUTHSECRET" value="xxxx"/>
		buffer[index++] = (byte)0x87;
		buffer[index++] = (byte)0x32; //name="AAUTHSECRET"
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(userpwd.getBytes(), 0, buffer, index, userpwd.length());
		index += userpwd.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of parm			
		buffer[index++] = (byte)0x01; //end of characteristic with the attribute 'APPLICATTION'
		
		return index - startPos;
	}
}
